//
//  ReplyuViewController.m
//  ECO
//
//  Created by zl on 2019/9/29.
//  Copyright © 2019 谷银行. All rights reserved.
//

#import "ReplyuViewController.h"
#import "ClearCell.h"
@interface ReplyuViewController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate,ClearCellDelegate>{
    UILabel *label;
    NSString *Type;
    NSString *Text;
    NSString *GroudID;
}
@property(nonatomic , strong)UITableView *ojtable;
@property(nonatomic , strong)UITextView *TextView;
@property (nonatomic, strong) NSMutableArray      *selectArray;//记录选择的所有选项

@end

@implementation ReplyuViewController
- (UITableView *)ojtable{
    if (!_ojtable) {
        _ojtable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        _ojtable.delegate = self;
        _ojtable.dataSource = self;
        _ojtable.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
        _ojtable.separatorColor = [UIColor whiteColor];
    }
    return _ojtable;
}
//- (UITextView *)TextView{
//    if (!_TextView) {
//
//        // 进入页面直接为输入状态
////        [_TextView becomeFirstResponder];// 2
//    }
//    return _TextView;
//}
- (NSMutableArray *)selectArray {
    if (!_selectArray) {
        _selectArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _selectArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.UpdataDic == nil) {
        self.navigationItem.title = @"添加快捷回复语";
    }else{
        self.navigationItem.title = @"修改快捷回复语";
    }
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(WanchengBtn)];
    [self.view addSubview:self.ojtable];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([Type isEqualToString:@"1"]) {
        return self.Array.count;
    }else{
        return 1;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([Type isEqualToString:@"1"]) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        }
        SetData *model = self.Array[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        
        cell.textLabel.text = model.groupName;
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        _TextView =[[UITextView alloc]initWithFrame:CGRectMake(15, 0, kScreenWidth - 30, 192)];
        _TextView.delegate = self;
        _TextView.font = [UIFont systemFontOfSize:15];
        [cell addSubview:self.TextView];
        if (self.UpdataDic == nil) {
            label = [[UILabel alloc]initWithFrame:CGRectMake(6, 4, 200, 20)];
            label.enabled = NO;
            label.text = @"请编辑快捷回复内容…";
            label.font =  [UIFont systemFontOfSize:15];
            label.textColor = [UIColor lightGrayColor];
            [self.TextView addSubview:label];
        }else{
            self.TextView.text = self.UpdataDic[@"recoverContent"];
        }
        return cell;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SetData *model = self.Array[indexPath.row];
    if ([Type isEqualToString:@"1"]) {
        self.TextView.hidden = NO;
        Type = @"";
        Text = model.groupName;
        GroudID = model.groupId;
        [self.ojtable reloadData];
    }else{
        return;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.Array.count > 0) {
        SetData *model = self.Array[section];
        UIView *View = [[UIView alloc]init];
        UIView *nameView = [[UIView alloc]initWithFrame:CGRectMake(0, 15, kScreenWidth, 44)];
        nameView.backgroundColor = [UIColor whiteColor];
        UIImageView *fuhao=[[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth - 14 -15), 15, 14, 14)];
        fuhao.image = [UIImage imageNamed:@"icon-next"];
        [nameView addSubview:fuhao];
        //将分组的名字nameLabel添加到nameview上
        UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 15, 100, 14)];
        [nameView addSubview:nameLabel];
        for (int i = 0; i<self.Array.count; i++) {
            SetData *data = self.Array[i];
            if ([Text isEqualToString:data.groupName]) {
                nameLabel.text = Text;
                break;
            }else{
                nameLabel.text = model.groupName;
            }
        }
        //添加一个button用于响应点击事件（展开还是收起）
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(0, 15, kScreenWidth, 44);
        [nameView addSubview:button];
        button.tag = section;
        [button addTarget:self action:@selector(Clicked:) forControlEvents:UIControlEventTouchUpInside];
        [View addSubview:nameView];
        return View;
    }else{
        UIView *View = [[UIView alloc]init];
        UIView *nameView = [[UIView alloc]initWithFrame:CGRectMake(0, 15, kScreenWidth, 44)];
        nameView.backgroundColor = [UIColor whiteColor];
        UIImageView *fuhao=[[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth - 14 -15), 15, 14, 14)];
        fuhao.image = [UIImage imageNamed:@"icon-next"];
        [nameView addSubview:fuhao];
        //将分组的名字nameLabel添加到nameview上
        UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 15, 100, 14)];
        [nameView addSubview:nameLabel];
        nameLabel.text = self.UpdataDic[@"recoverTitle"];
        [View addSubview:nameView];
        return View;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.UpdataDic == nil) {
        if ([Type isEqualToString:@"1"]) {
            return 59;
        }else{
            return 74;
        }
    }else{
        return 74;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([Type isEqualToString:@"1"]) {
        return 44;
    }else{
        return 192;
    }
}
- (void)Clicked:(UIButton *)send{
    Type = @"1";
    self.TextView.hidden = YES;
    [self.ojtable reloadData];
}
// 提示文字隐藏
- (void) textViewDidChange:(UITextView *)textView{
    if ([textView.text length] == 0) {
        [label setHidden:NO];
    }else{
        [label setHidden:YES];
    }
}
// 完成
- (void)WanchengBtn{
    NSUserDefaults *User = [NSUserDefaults standardUserDefaults];
    if (self.UpdataDic == nil) {
        if (!self.TextView.text) {
            return;
        }else{
            if (!Text) {
                SetData *model = self.Array[0];
                NSDictionary *dic = @{
                                    @"recoverTitle":model.groupName,
                                    @"kfUserId":[User objectForKey:@"kfId"],
                                    @"recoverContent":self.TextView.text,
                                    @"groupId":model.groupId
                                    };
                [HttpsTool post:@"/app/add/pri/recovery" params:dic success:^(id response) {
                    NSLog(@"%@",response);
                    NSLog(@"%@",response[@"msg"]);
                    [BRProgressHUD showErrorWithStatus:response[@"msg"]];
                    [BRProgressHUD dismissWithDelay:1.25];
                } failure:^(NSError *error) {
                    NSLog(@"%@",error);
                }];
            }else{
                NSDictionary *dic = @{
                                    @"recoverTitle":Text,
                                    @"kfUserId":[User objectForKey:@"kfId"],
                                    @"recoverContent":self.TextView.text,
                                    @"groupId":GroudID
                                    };
                [HttpsTool post:@"/app/add/pri/recovery" params:dic success:^(id response) {
                    NSLog(@"%@",response);
                    NSLog(@"%@",response[@"msg"]);
                    [BRProgressHUD showErrorWithStatus:response[@"msg"]];
                    [BRProgressHUD dismissWithDelay:1.25];
                } failure:^(NSError *error) {
                    NSLog(@"%@",error);
                }];
            }
            
        }
    }else{
        NSDictionary *dic = @{
                            @"recoverTitle":self.UpdataDic[@"recoverTitle"],
                            @"recoveryId":self.UpdataDic[@"recoveryId"],
                            @"recoverContent":self.TextView.text,
                            @"groupId":self.UpdataDic[@"groupId"]
                            };
        [HttpsTool post:@"/web/update/recovery" params:dic success:^(id response) {
            NSLog(@"%@",response);
            NSLog(@"%@",response[@"msg"]);
            [BRProgressHUD showErrorWithStatus:response[@"msg"]];
            [BRProgressHUD dismissWithDelay:1.25];
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
// 点击其他地方收回键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
@end
